<template>
    <div class="sidebar hb-custom">
        <el-menu
                class="sidebar-el-menu"
                :default-active="onRoutes"
                :collapse="sidebar.collapse"
                :background-color="sidebar.bgColor"
                :text-color="sidebar.textColor"
                router
        >
            <template v-for="item in menuData">
                <el-sub-menu v-if="item.children && item.children.length > 0" :index="item.index" :key="item.index">
                    <template #title>
                        <el-icon>
                            <component :is="item.icon"></component>
                        </el-icon>
                        <span>{{ item.title }}</span>
                    </template>
                    <el-menu-item v-for="subItem in item.children" :index="subItem.index" :key="subItem.index">
                        <template #title>{{ subItem.title }}</template>
                    </el-menu-item>
                </el-sub-menu>
                <el-menu-item v-else :index="item.index" :key="item.index">
                    <el-icon>
                        <component :is="item.icon"></component>
                    </el-icon>
                    <template #title>{{ item.title }}</template>
                </el-menu-item>
            </template>
        </el-menu>
    </div>
</template>

<script setup lang="ts">
import {computed} from "vue";
import {useSidebarStore} from "../store/sidebar";
import {useRoute} from "vue-router";
import {menuData} from "@/components/menu";

const route = useRoute();
const onRoutes = computed(() => {
    return route.path;
});

const sidebar = useSidebarStore();
</script>

<style scoped>
.sidebar {
    display: block;
    position: absolute;
    left: 0;
    top: 70px;
    bottom: 0;
    overflow-y: scroll;
}

.sidebar::-webkit-scrollbar {
    width: 0;
}

.sidebar-el-menu:not(.el-menu--collapse) {
    width: 221px;
}

.sidebar-el-menu {
    min-height: 100%;
}

/* 确保子菜单项与顶级菜单项具有一致的样式 */
:deep(.el-sub-menu .el-menu-item) {
    min-width: auto !important;
    height: 56px;
    line-height: 56px;
}

/* 确保子菜单标题样式与顶级菜单项一致 */
:deep(.el-sub-menu .el-sub-menu__title) {
    height: 56px;
    line-height: 56px;
}
</style>